<template>
    <v-dialog v-model="dialogFormVisible" max-width="900px" persistent scrollable >
        <div style=" overflow-y: scroll; " class="h-100 rounded-lg">
            <v-card class="elevation-10 pa-2"  >
            <v-card-title class="d-flex align-center">
                <v-icon class="mr-2">mdi-file-document-edit</v-icon>
                {{ title }}
            </v-card-title>

            <!-- 步骤指示器 -->
            <v-stepper v-model="currentStep" class="elevation-0">
                <v-stepper-header class="elevation-0">
                    <template v-for="(step, index) in steps" :key="index">
                        <v-stepper-item
                            :value="index + 1"
                            :title="step.title"
                            :subtitle="step.subtitle"
                            :complete="currentStep > index + 1"
                        >
                            <template v-slot:icon>
                                <v-icon v-if="currentStep > index + 1">mdi-check</v-icon>
                                <span v-else>{{ index + 1 }}</span>
                            </template>
                        </v-stepper-item>
                        <v-divider v-if="index < steps.length - 1"></v-divider>
                    </template>
                </v-stepper-header>

                <v-stepper-window>
                    <!-- 步骤1：填写数据 -->
                    <v-stepper-window-item :value="1" >
                        <div class="" >
                            <v-form ref="formRef" v-model="formValid">
                                <!-- 基本信息 -->

                                <v-card class="mb-4" variant="outlined">
                                    <v-card-title class="text-subtitle-1 bg-light-blue-lighten-5 ">
                                        <v-icon class="mr-2">mdi-book-open-variant</v-icon>
                                        基本信息
                                    </v-card-title>
                                    <v-card-text>
                                        <v-row>
                                            <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <!-- <v-text-field 
                                                    v-model="form.customerId" 
                                                    label="客户信息"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.customerId"
                                                    required 
                                                    hide-details="auto" 
                                                /> -->
                                                <CustomerSelect
                                                    v-model="form.customerId"
                                                    label="客户信息"
                                                    variant="outlined"
                                                    density="compact"
                                                    :rules="rules.customerId"
                                                    required
                                                    @select="handleCustomerSelect"
                                                    hide-details="auto"
                                                />
                                            </v-col>
                                            
                                            <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <v-text-field 
                                                    v-model="form.contractCode" 
                                                    label="合同编号"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.contractCode"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                                <div style="font-size: 12px; color: #666;">可自由填写</div>
                                            </v-col>
                                        </v-row>
                                        
                                    </v-card-text>
                                </v-card>
                           


                                <!-- 课程信息 -->
                                <v-card class="mb-4" variant="outlined">
                                    <v-card-title class="text-subtitle-1 bg-green-lighten-5">
                                        <v-icon class="mr-2">mdi-book-open-variant</v-icon>
                                        课程信息
                                    </v-card-title>
                                    <v-card-text>
                                        <v-row>
                                            <v-col cols="12" xs="12" sm="5" md="3" lg="3" xl="3">
                                                <v-text-field 
                                                    v-model="form.courseId" 
                                                    label="课程ID"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.courseId"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                            <v-col cols="12" xs="12" sm="6" md="3" lg="3" xl="3">
                                                <v-text-field 
                                                    v-model="form.coursePrice" 
                                                    label="课程金额"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.coursePrice"
                                                    type="number"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>

                                            <v-col cols="12" xs="12" sm="6" md="2" lg="2" xl="2">
                                                <v-text-field 
                                                    v-model="form.discountPrice" 
                                                    label="优惠金额"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.discountPrice"
                                                    type="number"
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                            <v-col cols="12" xs="12" sm="6" md="2" lg="2" xl="2">
                                                <v-text-field 
                                                    v-model="form.courseNumber" 
                                                    label="课程课时"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.courseNumber"
                                                    type="number"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                            
                                            <v-col cols="12" xs="12" sm="6" md="2" lg="2" xl="2">
                                                <v-text-field 
                                                    v-model="form.freeNumber" 
                                                    label="赠送课时"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.freeNumber"
                                                    type="number"
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                        </v-row>
                                    </v-card-text>
                                </v-card>

                                <!-- 付款信息 -->
                                <v-card class="mb-4" variant="outlined">
                                    <v-card-title class="text-subtitle-1 bg-orange-lighten-5">
                                        <v-icon class="mr-2">mdi-credit-card</v-icon>
                                        付款信息
                                    </v-card-title>
                                    <v-card-text>
                                        <v-row>
                                            <v-col cols="12" xs="12" sm="3" md="3" lg="3" xl="3">
                                                <v-text-field 
                                                    v-model="form.paymentPrice" 
                                                    label="付款金额"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.paymentPrice"
                                                    type="number"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                            <v-col cols="12" xs="12" sm="2" md="2" lg="2" xl="3">
                                                <v-text-field 
                                                    v-model="form.paymentMethod" 
                                                    label="付款方式"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.paymentMethod"
                                                    type="number"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                            <v-col cols="12" xs="12" sm="3" md="3" lg="3" xl="3">
                                                <v-text-field 
                                                    v-model="form.paymentType" 
                                                    label="付款类型"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.paymentType"
                                                    type="number"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                            <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <v-text-field 
                                                    v-model="form.paymentRemark" 
                                                    label="付款备注"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.paymentRemark"
                                                    hide-details="auto" 
                                                />
                                            </v-col>
                                        </v-row>
                                    </v-card-text>
                                </v-card>

                                <!-- 审批信息 -->
                                <v-card class="mb-4" variant="outlined">
                                    <v-card-title class="text-subtitle-1 bg-purple-lighten-5">
                                        <v-icon class="mr-2">mdi-clipboard-check</v-icon>
                                        审批信息
                                    </v-card-title>
                                    <v-card-text>
                                        <v-row>
                                            <!-- <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <v-select 
                                                    v-model="form.status" 
                                                    :items="dict['approval_status']"
                                                    item-title="dictName" 
                                                    item-value="dictValue" 
                                                    label="状态"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.status"
                                                    required 
                                                    clearable
                                                    details="auto"
                                                    hide-details="auto" 
                                                />
                                            </v-col> -->
                                            <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <v-text-field 
                                                    v-model="form.type" 
                                                    label="审批类型"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.type"
                                                    type="number"
                                                    required 
                                                    hide-details="auto" 
                                                />
                                            </v-col>

                                            <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <v-text-field 
                                                    v-model="form.enclosureJson" 
                                                    label="附件"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.enclosureJson"
                                                    hide-details="auto" 
                                                />
                                            </v-col>


                                            <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                                <v-textarea 
                                                    v-model="form.remark" 
                                                    label="备注"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.remark"
                                                    rows="3"
                                                    hide-details="auto" 
                                                />
                                            </v-col>

                                            <v-col cols="12" xs="12" sm="6" md="6" lg="6" xl="6">
                                                <!-- <v-text-field 
                                                    v-model="form.performanceBelonging" 
                                                    label="业绩归属"
                                                    variant="outlined" 
                                                    density="compact" 
                                                    :rules="rules.performanceBelonging"
                                                    required 
                                                    hide-details="auto" 
                                                /> -->

                                                <!-- 业绩归属 多人百分比分配 -->
                                        <div class="mt-2">
                                            <div class="mb-1 font-weight-bold d-flex align-center gap-2">业绩归属

                                                <v-btn  color="primary" size="small" @click="addPerformance"  >添加</v-btn>

                                            </div>
                                            <!-- SelectUser组件选择用户 -->
                                            <SelectUser
                                                v-model="selectedUsers"
                                                label="选择用户"
                                                :multiple="true"
                                                hide-details="auto"
                                                placeholder="可多选"
                                                variant="outlined"
                                                density="comfortable"
                                                prepend-inner-icon="mdi-account-arrow-right"
                                                class="mb-3"
                                                @user-selected-list="handleUserSelect"
                                            />
                                            <div v-for="(item, idx) in performanceList" :key="item.i" class="d-flex align-center mb-2 gap-2">
                                                <!-- 用户名 -->
                                                <v-text-field
                                                    v-model="item.n"
                                                    label="用户名"
                                                    dense
                                                    hide-details
                                                    required
                                                    readonly
                                                />
                                                <v-text-field
                                                    v-model="item.p"
                                                    label="百分比"
                                                    type="number"
                                                    style="max-width: 130px"
                                                    dense
                                                    hide-details
                                                    required
                                                    suffix="%"
                                                    min="0"
                                                    max="100"
                                                />
                                                <v-btn  color="error" size="small" variant="text" @click="removePerformance(idx)" v-if="performanceList.length > 1" >删除</v-btn>
                                            </div>


                                            <div v-if="performanceError" class="text-error text-caption">{{ performanceError }}</div>
                                        </div>
                                            </v-col>
                                            
                                          
                                        </v-row>
                                    </v-card-text>
                                </v-card>
                            </v-form>
                        </div>
                            
                        
                       
                    </v-stepper-window-item>

                    <!-- 步骤2：确认数据 -->
                    <v-stepper-window-item :value="2">
                        <v-card-text>
                            <v-card class="mb-4" variant="outlined">
                                <v-card-title class="text-subtitle-1 bg-blue-lighten-5">
                                    <v-icon class="mr-2">mdi-eye</v-icon>
                                    请确认以下信息
                                </v-card-title>
                                <v-card-text>
                                    <v-row>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="primary">mdi-account</v-icon>
                                                </template>
                                                <v-list-item-title>客户ID</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.customerId || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="primary">mdi-account-school</v-icon>
                                                </template>
                                                <v-list-item-title>学生ID</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.studentId || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="primary">mdi-file-document</v-icon>
                                                </template>
                                                <v-list-item-title>合同编号</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.contractCode || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="primary">mdi-account-group</v-icon>
                                                </template>
                                                <v-list-item-title>业绩归属</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.performanceBelonging || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="success">mdi-book</v-icon>
                                                </template>
                                                <v-list-item-title>课程ID</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.courseId || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="success">mdi-currency-usd</v-icon>
                                                </template>
                                                <v-list-item-title>课程金额</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.coursePrice || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="success">mdi-clock</v-icon>
                                                </template>
                                                <v-list-item-title>课程课时</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.courseNumber || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="warning">mdi-credit-card</v-icon>
                                                </template>
                                                <v-list-item-title>付款金额</v-list-item-title>
                                                <v-list-item-subtitle>{{ form.paymentPrice || '未填写' }}</v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                        <v-col cols="12" xs="12" sm="6" md="4" lg="4" xl="3">
                                            <v-list-item>
                                                <template v-slot:prepend>
                                                    <v-icon color="info">mdi-clipboard-check</v-icon>
                                                </template>
                                                <v-list-item-title>审批状态</v-list-item-title>
                                                <v-list-item-subtitle>
                                                    {{ getStatusText(form.status) || '未选择' }}
                                                </v-list-item-subtitle>
                                            </v-list-item>
                                        </v-col>
                                    </v-row>
                                </v-card-text>
                            </v-card>
                        </v-card-text>
                    </v-stepper-window-item>

                    <!-- 步骤3：成功提示 -->
                    <v-stepper-window-item :value="3">
                        <v-card-text class="text-center">
                            <v-card class="mb-4" variant="outlined">
                                <v-card-text class="py-8">
                                    <v-icon size="64" color="success" class="mb-4">mdi-check-circle</v-icon>
                                    <h3 class="text-h5 mb-2">提交成功！</h3>
                                    <p class="text-body-1 text-medium-emphasis">
                                        您的报名审批申请已成功提交，请等待审批结果。
                                    </p>
                                    <v-chip color="primary" class="mt-4">
                                        <v-icon start>mdi-clock</v-icon>
                                        审批编号：{{ submitResult?.id || '生成中...' }}
                                    </v-chip>
                                </v-card-text>
                            </v-card>
                        </v-card-text>
                    </v-stepper-window-item>
                </v-stepper-window>
            </v-stepper>

            <!-- 底部按钮 -->
            <v-card-actions>
                <v-spacer />
                <v-btn 
                    v-if="currentStep > 1" 
                    color="grey-darken-1" 
                    variant="text" 
                    @click="prevStep"
                >
                    上一步
                </v-btn>
                <v-btn 
                    v-if="currentStep < 3" 
                    color="primary" 
                    variant="text" 
                    :loading="saving" 
                    @click="nextStep"
                >
                    {{ currentStep === 1 ? '下一步' : '确认提交' }}
                </v-btn>
                <v-btn 
                    v-if="currentStep === 3" 
                    color="primary" 
                    variant="text" 
                    @click="close"
                >
                    完成
                </v-btn>
                <v-btn 
                    v-if="currentStep < 3" 
                    color="grey-darken-1" 
                    variant="text" 
                    @click="close"
                >
                    取消
                </v-btn>
            </v-card-actions>
        </v-card>
        </div>
      
    </v-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick, onMounted, computed, watch } from 'vue';
import {
    doInsert,
    doUpdate,
    type ApprovalSignModel,
    type ApiResponse
} from '@/api/approval/sign/ApprovalSignManagementApi';
import { isNull } from '@/utils/validate';
import { formateDate } from '@/utils/format';
import { validatorRule } from '@/utils/validateRule';
import { getDictListSync } from '@/utils/dict';
import { useMessage } from '@/composables/useMessage';
import SelectUser from '@/components/SelectUser.vue';
import type { UserInfo } from '@/store/user';
import CustomerSelect from '@/views/modules/customer/customerrelation/components/CustomerSelect.vue';
import type { CustomerStudentRelationModel } from '@/api/customer/customerrelation/CustomerStudentRelationManagementApi';
const { success: showSuccess, error: showError } = useMessage();

// 步骤配置
const steps = [
    { title: '填写数据', subtitle: '填写审批信息' },
    { title: '确认数据', subtitle: '确认提交信息' },
    { title: '提交成功', subtitle: '审批已提交' }
];

// 基础数据
const dialogFormVisible = ref(false);
const title = ref('');
const formValid = ref(false);
const formRef = ref();
const saving = ref(false);
const currentStep = ref(1);
const submitResult = ref<ApprovalSignModel | null>(null);
const customerList = ref<CustomerStudentRelationModel[]>([]);

// 默认数据 合同编号 毫秒级时间戳
const defaultFrom: ApprovalSignModel = {
    customerId: '',
    studentId: '',
    contractCode: 'GuaClass-'+formateDate(new Date(), 'yyyyMMddhhmmssSSS'),//毫秒级时间戳
    performanceBelonging: '',
    status: '',
    enclosureJson: '',
    courseId: '',
    coursePrice: undefined,
    courseNumber: undefined,
    discountPrice: undefined,
    freeNumber: undefined,
    remark: '',
    paymentPrice: undefined,
    paymentMethod: undefined,
    paymentType: undefined,
    type: undefined,
    paymentRemark: '',
}

// 表单数据
const form = reactive<Partial<ApprovalSignModel>>(defaultFrom);

// 字典数据
const dict = reactive({
    'approval_status': [] as any[],
});

// 验证规则
const rules = {
    customerId: [
        (v: any) => !!v || '客户ID不能为空',
    ],
    studentId: [],
    contractCode: [
        (v: any) => !!v || '合同编号不能为空',
    ],
    performanceBelonging: [
        (v: any) => !!v || '业绩归属不能为空',
    ],
    status: [
        (v: any) => !!v || '状态不能为空',
    ],
    enclosureJson: [],
    courseId: [
        (v: any) => !!v || '课程ID不能为空',
    ],
    coursePrice: [
        (v: any) => !!v || '课程金额不能为空',
        (v: any) => !v || validatorRule.IS_MONEY(v) || '请输入正确的课程金额',
    ],
    courseNumber: [
        (v: any) => !!v || '课程课时不能为空',
        (v: any) => !v || validatorRule.IS_POSITIVE_INTEGER(v) || '请输入正确的课时数',
    ],
    discountPrice: [
        (v: any) => !v || validatorRule.IS_MONEY(v) || '请输入正确的优惠金额',
    ],
    freeNumber: [
        (v: any) => !v || validatorRule.IS_POSITIVE_INTEGER(v) || '请输入正确的赠送课时',
    ],
    remark: [],
    paymentPrice: [
        (v: any) => !!v || '付款金额不能为空',
        (v: any) => !v || validatorRule.IS_MONEY(v) || '请输入正确的付款金额',
    ],
    paymentMethod: [
        (v: any) => !!v || '付款方式不能为空',
    ],
    paymentType: [
        (v: any) => !!v || '付款类型不能为空',
    ],
    type: [
        (v: any) => !!v || '审批类型不能为空',
    ],
    paymentRemark: [],
};

interface PerformanceUser {
  n: string; // 用户名
  i: string; // 用户ID
  p: number; // 百分比
}

const performanceList = ref<PerformanceUser[]>([]);
const performanceError = ref('');
const selectedUsers = ref<any[]>([]);

watch(performanceList, (val) => {
  // 校验总和
  const total = val.reduce((sum, item) => sum + Number(item.p || 0), 0);
  if (total !== 100) {
    performanceError.value = '所有用户分配比例之和必须为100%';
  } else {
    performanceError.value = '';
  }
  // 同步到form.performanceBelonging
  form.performanceBelonging = JSON.stringify(val);
}, { deep: true });

function addPerformance() {
  performanceList.value.push({ n: '', i: '', p: 0 });
}
function removePerformance(idx: number) {
  if (performanceList.value.length > 1) {
    performanceList.value.splice(idx, 1);
  }
}

// 处理客户选择
function handleCustomerSelect(list: CustomerStudentRelationModel[]) {
    if(list.length > 0) {
        customerList.value = list;
        form.customerId = list[0].id;
        form.studentId = list[0].students?.[0]?.id;
    }
}

// 处理SelectUser选择
function handleUserSelect(userList: UserInfo[]) {
  // userList: [{id, name}]
  // 保持顺序，已存在的保留百分比
  const newList = userList.map(user => {
    const exist = performanceList.value.find(item => item.i == user.id);
    console.log(user);
    return {
      n: user.realName,
      i: user.id,
      p: exist ? exist.p : 0
    };
  });
  performanceList.value = newList;
}

// 事件发射
const emit = defineEmits<{
    fetchData: [];
}>();

// 获取状态文本
function getStatusText(status: string | undefined): string {
    if (!status) return '';
    const statusItem = dict.approval_status.find(item => item.dictValue === status);
    return statusItem ? statusItem.dictName : status;
}

// 下一步
async function nextStep() {
    if (currentStep.value === 1) {
        // 验证表单
        if (!formRef.value) return;
        const { valid } = await formRef.value.validate();
        if (!valid) return;
        currentStep.value = 2;
    } else if (currentStep.value === 2) {
        // 提交数据
        await submitData();
    }
}

// 上一步
function prevStep() {
    if (currentStep.value > 1) {
        currentStep.value--;
    }
}

// 提交数据
async function submitData() {
    try {
        saving.value = true;

        // 处理表单数据
        const submitData = { ...form } as ApprovalSignModel;
        handlerFormData(submitData);

        let response: ApiResponse<ApprovalSignModel>;

        // 编辑模式
        if (!isNull(submitData.id)) {
            response = await doUpdate(submitData);
            showSuccess(response.msg || '更新成功');
        } else {
            // 新增模式
            response = await doInsert(submitData);
            showSuccess(response.msg || '添加成功');
        }

        // 保存提交结果
        submitResult.value = response.data || submitData;
        
        // 进入成功页面
        currentStep.value = 3;

        // 通知父组件刷新数据
        emit('fetchData');
    } catch (error) {
        console.error('save failed:', error);
        showError('保存失败');
    } finally {
        saving.value = false;
    }
}

// 显示编辑对话框
function showEdit(row?: ApprovalSignModel) {
    if (!row) {
        title.value = '添加报名审批';
        // 重置表单为默认值
        Object.assign(form, defaultFrom);
    } else {
        title.value = '编辑报名审批';
        // 复制行数据到表单，处理日期格式
        const formData = { ...row };
        Object.assign(form, formData);
    }

    // 重置步骤
    currentStep.value = 1;
    submitResult.value = null;
    dialogFormVisible.value = true;
}

// 关闭对话框
function close() {
    dialogFormVisible.value = false;
    nextTick(() => {
        if (formRef.value) {
            formRef.value.reset();
        }
        // 重置表单数据
        Object.assign(form, defaultFrom);
        // 重置步骤
        currentStep.value = 1;
        submitResult.value = null;
    });
}

// 处理表单数据
function handlerFormData(formData: any) {
    if (!isNull(formData)) {
        for (const key in formData) {
            // 对于时间类进行处理
            if (Object.prototype.toString.call(formData[key]) === '[object Date]') {
                formData[key] = formateDate(formData[key], 'yyyy-MM-dd hh:mm:ss');
            }
            // 处理datetime-local格式
            else if (typeof formData[key] === 'string' && formData[key].includes('T')) {
                formData[key] = formData[key].replace('T', ' ') + ':00';
            }
        }
    }
}

// 初始化字典数据
function initDict() {
    dict.approval_status = getDictListSync('approval_status') || [];
}

// 暴露方法
defineExpose({
    showEdit,
});

// 生命周期
onMounted(() => {
    initDict();
});
</script>

<style lang="scss" scoped>




.no-jump-field .v-messages {
  position: absolute; /* 将错误提示脱离文档流 */
  min-height: 0 !important;
}
.no-jump-field .v-messages__message {
  line-height: 1.2;
}
:deep(.v-dialog) {
    .v-card-title {
        background-color: rgb(var(--v-theme-primary));
        color: white;
    }
}

:deep(.v-stepper) {
    .v-stepper-header {
        box-shadow: none;
    }
}

.bg-light-blue-lighten-5 {
    background-color: #e3f2fd !important;
}

.bg-green-lighten-5 {
    background-color: #e8f5e8 !important;
}

.bg-orange-lighten-5 {
    background-color: #fff3e0 !important;
}

.bg-purple-lighten-5 {
    background-color: #f3e5f5 !important;
}

.bg-blue-lighten-5 {
    background-color: #e3f2fd !important;
}
</style>